/*
 * Created on 07-feb-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.juanfrivaldes.cio2005.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.springframework.mail.MailException;

import com.juanfrivaldes.cio2005.domain.Usuario;

/**
 * @author root
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class OlvidoAction extends BaseAction {

	private static Log log = LogFactory
			.getLog(AutentificacionUsuarioAction.class);

	protected ActionForward protectedExecute(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		OlvidoForm olvidoForm = (OlvidoForm) form;

		if (isCancelled(request)) {
			return mapping.findForward("inicio");
		}

		try {

			Usuario usr = null;

			usr = this.getCio2005().getUsuario(olvidoForm.getEmail());

			if (usr == null) {
				ActionMessages errors = new ActionErrors();
				errors.add("email", new ActionMessage("olvido.email.problema"));
				log.trace("Usuario" + olvidoForm.getEmail()
						+ " en página de olvido no existe.\n");
				this.saveErrors(request, errors);
				return new ActionForward(mapping.getInput());
			}

			getCio2005().sendMailOlvido(usr);

		} catch (MailException e) {
			log.trace("Envio Fallido de contraseña a la dirección "
					+ olvidoForm.getEmail() + ":\n" + e.getMessage());
		} catch (Exception e) {
			log.trace("fallo en la base de datos");
			return mapping.findForward("dataFailure");
		}
		return mapping.findForward("success");
	}
}
